#include <jni.h>
#include <exception>
#include <android/log.h>
#include <stddef.h>
#include <string>
#include <sstream>

using namespace std;

class MyException:public std::exception{

public:
    const char* what() const _GLIBCXX_USE_NOEXCEPT{
        return "自定义错误被抛出了";
    }
};

extern "C"
JNIEXPORT void Java_libs_yzx_zaza_JNIActivity_exceptionTest(JNIEnv *env,jclass jc){

    try {
        MyException * e = new MyException;
        throw  e;
    }
    /* 抛出指针,用指针catch */
    catch (MyException *e){
        __android_log_write(ANDROID_LOG_ERROR,"LOG------JNI",e->what());
    }


    try {
        throw string("主动抛出个文字错误");
    }catch (std::string e){
        __android_log_write(ANDROID_LOG_ERROR,"LOG------JNI",e.c_str());
    }


    try {
        string a = NULL;
        stringstream ss;
        ss<<a.c_str();
        __android_log_write(ANDROID_LOG_ERROR,"LOG------JNI",ss.str().c_str());
    }catch (...){
        __android_log_write(ANDROID_LOG_ERROR,"LOG------JNI","未知错误发生");
    }
}
